﻿/*
 * Tencent is pleased to support the open source community by making xLua available.
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using XLua;
using System;
using System.IO;
using UnityEngine.UI;
using UnityEngine.Networking;

namespace XLuaTest
{
    [System.Serializable]
    public class Injection
    {
        public string name;
        public GameObject value;
    }

    [LuaCallCSharp]
    public class LuaBehaviour : MonoBehaviour
    {
        public Text m_text;

        LuaEnv luaenv = null;

        [CSharpCallLua]
        public delegate string FDelegate(int a, string b);

        void Start()
        {
            Debug.LogError("LuaBehaviour Start");
            luaenv = new LuaEnv();
            luaenv.AddLoader(CustomLoader);
            luaenv.DoString("require 'LuaTestScript'");
            FDelegate f = luaenv.Global.Get<FDelegate>("f");
            string f_ret = f(100, "John");
            m_text.text = f_ret;
        }

        /// <summary>
        /// 自定义的路径的加载器
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        private byte[] CustomLoader(ref string filename)
        {
            Debug.LogError("persistentDataPath1=" + Application.persistentDataPath);
            string path = string.Empty;
            if (Application.platform == RuntimePlatform.Android)
            {
                path = string.Format("{0}/{1}.lua.txt", Application.persistentDataPath, filename);
                Debug.LogError("persistentDataPath2=" + path);
            }
            else
            {
                path = string.Format("{0}/StreamingAssets/lua/64/{1}.lua.txt", Application.dataPath, filename);
            }
            return File.ReadAllBytes(path);
        }

        void OnDestroy()
        {
            luaenv.Dispose();
        }

        public void CopyFile32()
        {
            Debug.LogError("CopyFile32");
            string destPath = Application.persistentDataPath + "/LuaTestScript.lua.txt";
            if (File.Exists(destPath))
            {
                File.Delete(destPath);
            }

            string filePath = Application.streamingAssetsPath + "/lua/32/LuaTestScript.lua.txt";
            if (File.Exists(filePath))
            {
                int a = 1;
                int b = 2;
            }

            CopyFile(filePath, destPath);
        }

        public void CopyFile64()
        {
            Debug.LogError("CopyFile64");
            string destPath = Application.persistentDataPath + "/LuaTestScript.lua.txt";
            if (File.Exists(destPath))
            {
                File.Delete(destPath);
            }
            CopyFile(Application.streamingAssetsPath + "/lua/64/LuaTestScript.lua.txt", destPath);
        }

        public void CopyFileText()
        {
            Debug.LogError("CopyFileText");
            string destPath = Application.persistentDataPath + "/LuaTestScript.lua.txt";
            if (File.Exists(destPath))
            {
                File.Delete(destPath);
            }
            CopyFile(Application.streamingAssetsPath + "/lua/LuaTestScript.lua.txt", destPath);
        }

        private void CopyFile(string filePath, string destPath)
        {
            WWW www = new WWW(filePath);
            while (!www.isDone) { }
            File.WriteAllBytes(destPath, www.bytes);
            Debug.LogError("CopyFilesrc:" + filePath);
            Debug.LogError("CopyFiledest:" + destPath);
        }
    }
}
